<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<h1>test.html</h1>


<button style="padding: 20px;  " onclick="loadDoc()">loadDoc</button>
<button style="padding: 20px;  " onclick="aaa()">aaa</button>

<p id="demo"></p>


<style>
    .title {
        color: red;
    }
</style>
<div class="box"></div>


<script th:inline="javascript">


    var myData;

    function loadDoc() {
        const xhttp = new XMLHttpRequest();
        xhttp.open("GET", "/ajaxtest/json");
        xhttp.send();
        xhttp.onload = function () {
            // document.getElementById("demo").innerHTML = this.responseText;
            myData = this.responseText
        }
    }

    function aaa() {
        console.log(JSON.parse(myData));
        let bbb = JSON.parse(myData)
        let html;
        for (var i = 0; i < bbb.length; i++) {
            html =  html +  ` <h2  class="title"> ${bbb[i].title} </h2> `
            console.log(html);
        }
        document.getElementsByClassName("box")[0].innerHTML = html;
    }

</script>
</body>
</html>